/*******************************************************************************
 * Copyright (c) 2011 Jovansonlee B. Cesar.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Jovansonlee B. Cesar - initial API and implementation
 ******************************************************************************/
package com.ivanceras.client.widgets;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.ivanceras.client.DataService;
import com.ivanceras.client.DataServiceAsync;
import com.ivanceras.shared.bo.Ad_column;
import com.ivanceras.shared.bo.Ad_field;
import com.ivanceras.shared.bo.Ad_tab;
import com.ivanceras.shared.bo.Ad_table;
import com.ivanceras.shared.bo.Ad_window;
import com.ivanceras.shared.db.Filter;

public class WindowController {
	private final DataServiceAsync dataService = GWT
	.create(DataService.class);


	@UiField DivElement windowName;
	@UiField HTMLPanel windowPanel;

	AdTabUi[] tabUiList = null;
	AdTabUi parentTabUi = null;


	protected Ad_tab[] ad_tabList;


	/**
	 * load the tabs for this window, to build a window user interface based on the ad_tabs, field, table and column definitions
	 * @param window
	 * @param loadCallback
	 */
	public void load(Ad_window window, AsyncCallback<AdWindowUi> loadCallback) {
		retrieveTabList(window, loadCallback);
	}

	private void retrieveTabList(final Ad_window window, final AsyncCallback<AdWindowUi> loadCallback){
		dataService.retriveTabList(window, new AsyncCallback<Ad_tab[]>(){

			@Override
			public void onFailure(Throwable caught) {
				loadCallback.onFailure(caught);
			}

			@Override
			public void onSuccess(Ad_tab[] result) {
				ad_tabList = result;
				tabUiList = new AdTabUi[result.length];
				for(int i = 0; i < result.length; i++){
					Ad_tab tab = result[i];
					AdTabUi tabui = new AdTabUi(tab, false);
					tabui.setWindowController(WindowController.this);
					tabUiList[i] = tabui;
				}
				AdWindowUi windowUi = new AdWindowUi(tabUiList);
				loadCallback.onSuccess(windowUi);
			}

		});
	}

	public Filter getFilter(AdTabUi tabUi){
		//AdTabUi tabUi = tabList.get(tabIndex);
		Ad_tab adtab = tabUi.getAd_tab();
		Ad_column pcolumn = getParentColumn(ad_tabList, adtab);
		Filter filter = null;
		System.out.println("Getting filter for this tab: "+tabUi.getAd_tab().getName()+" with column that match: "+(pcolumn != null ? pcolumn.getColumnname() : null));
		if(pcolumn != null){
			String columnName = pcolumn.getColumnname();
			int ptabIndex = getParentTabIndex(pcolumn);
			parentTabUi = tabUiList[ptabIndex];
			Object value = parentTabUi.getFieldValue(columnName);
			filter = new Filter(columnName.toLowerCase(), Filter.EQUAL, value);

			System.out.println("Filter will be: "+filter.toString());
		}
		return filter;
	}

	private int getParentTabIndex(Ad_column column){
		String columnName = column.getColumnname();
		for(int i = 0; i < tabUiList.length; i++){
			Ad_tab adtab = tabUiList[i].getAd_tab();
			Ad_field[] fieldList = adtab.getAd_fieldList();
			for(Ad_field f : fieldList){
				String fcolumn = f.getAd_column().getColumnname();
				if(columnName.equals(fcolumn)){
					return i;
				}
			}
		}
		return -1;
	}

	public AdTabUi getParentTabUi(){
		return parentTabUi;
	}

	private Ad_column getParentColumn(Ad_tab[] tabList, Ad_tab tab){
		String [] primaryKeys = getPrimarykeys(tabList);
		Ad_field[] fieldList = tab.getAd_fieldList();
		for(Ad_field field : fieldList){
			Ad_column column = field.getAd_column();
			if(inPrimaryKeys(primaryKeys, column.getColumnname())){
				return column;
			}
		}
		return null;
	}

	private boolean inPrimaryKeys(String[] primaryKeys, String column){
		for(String pk : primaryKeys){
			if(pk.equals(column)){
				return true;
			}
		}
		return false;
	}

	private String[] getPrimarykeys(Ad_tab[] tabList) {
		List<String> primaryKeys = new ArrayList<String>();
		for(Ad_tab tab : tabList){
			Ad_table table  = tab.getAd_table();
			Ad_column[] columnList = table.getAd_columnList();
			if(columnList != null){
				for(Ad_column col : columnList){
					if(col.getIskey().equals("Y")){
						primaryKeys.add(col.getColumnname());
					}
				}
			}
		}
		return primaryKeys.toArray(new String[primaryKeys.size()]);
	}


}
